﻿<ComClass(ClsVlCurve.ClassId, ClsVlCurve.InterfaceId, ClsVlCurve.EventsId)>
Public Class ClsVlCurve

#Region "COM GUID"
    ' 这些 GUID 提供此类的 COM 标识 
    ' 及其 COM 接口。若更改它们，则现有的
    ' 客户端将不再能访问此类。
    Public Const ClassId As String = "e5051caa-cbb4-4288-a8fb-8daec1449480"
    Public Const InterfaceId As String = "9964b1fc-6fe5-4744-b4d5-a7e80471a60c"
    Public Const EventsId As String = "4eba6a21-eb5e-4e5f-9b87-eb218c722ae2"
#End Region

    ' 可创建的 COM 类必须具有一个不带参数的 Public Sub New() 
    ' 否则， 将不会在 
    ' COM 注册表中注册此类，且无法通过
    ' CreateObject 创建此类。
    Public Sub New()
        MyBase.New()
    End Sub


    Private objVLAX As ClsVlax
    Private mvarEntity As ClsAcadEntity
    Private types(0 To 8) As String

    Public Sub IntiClass(AcApp As Object)

        objVLAX = New ClsVlax
        objVLAX.IntiClass(AcApp)
        types(0) = "AcDbCircle" : types(1) = "AcDbLine"
        types(2) = "AcDbArc" : types(3) = "AcDbSpline"
        types(4) = "AcDb3dPolyline" : types(5) = "AcDbPolyline"
        types(6) = "AcDb2dPolyline" : types(7) = "AcDbEllipse"
        types(8) = "AcDbLeader"

    End Sub

    Public Property Entity() As ClsAcadEntity
        Get
            Dim rtn As New ClsAcadEntity
            rtn.CteateFromCad(mvarEntity)
            Return rtn
        End Get
        Set(ent As ClsAcadEntity)
            Debug.Assert(types.Contains(ent.ObjectName), "That entity is not a curve")
            Me.mvarEntity = ent
        End Set
    End Property

    Public ReadOnly Property CurveType() As String
        Get
            Return Me.Entity.ObjectName
        End Get

    End Property

    Public ReadOnly Property Area() As Double
        Get
            Dim retval As Double
            With Me.objVLAX
                .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
                retval = .EvalLispExpression("(vlax-curve-getArea (handent handle))")
                .NullifySymbol({"handle"})
            End With
            Return retval
        End Get
    End Property
    Public ReadOnly Property Closed() As Boolean
        Get
            Dim retval As Boolean
            With Me.objVLAX
                .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
                retval = .EvalLispExpression("(vlax-curve-isClosed (handent handle))")
                .NullifySymbol({"handle"})
            End With
            Return retval
        End Get
    End Property
    Public ReadOnly Property EndParameter() As Double
        Get
            Dim retval As Double
            With Me.objVLAX
                .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
                retval = .EvalLispExpression("(vlax-curve-getEndParam (handent handle))")
                .NullifySymbol({"handle"})
            End With
            Return retval
        End Get
    End Property

    Public ReadOnly Property EndPoint() As ClsPoint3d
        Get
            Dim retval As Object, rtn As New ClsPoint3d
            With Me.objVLAX
                .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
                .EvalLispExpression("(setq lst (vlax-curve-getEndPoint (handent handle)))")
                retval = .GetLispList("lst")
                .NullifySymbol({"handle", "lst"})
            End With
            rtn.CreatorByVariant(retval)
            Return rtn
        End Get
    End Property

    Public Function GetClosestPointTo(point As Object, Optional Extend As Boolean = False) As ClsPoint3d

        Dim retval As Object, pt As New ClsPoint3d
        Dim i As Long
        With objVLAX
            .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
            .SetLispSymbol("givenPt", point)
            If Extend Then .EvalLispExpression("(setq ext T)")
            .EvalLispExpression("(setq lst (vlax-curve-getClosestPointTo (handent handle) givenPt ext))")
            retval = .GetLispList("lst")
            .NullifySymbol({"handle", "lst", "ext", "givenPt"})
        End With
        pt.CreatorByVariant(retval)
        Return pt
    End Function

    Public Function GetDistanceAtParameter(Param As Double) As Double

        Dim retval As Double

        With objVLAX
            .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
            .SetLispSymbol("param", Param)
            retval = .EvalLispExpression("(vlax-curve-getDistAtParam (handent handle) param)")
            .NullifySymbol({"handle", "param"})
        End With
        Return retval

    End Function

    Public Function GetDistanceAtPoint(point As Object) As Double

        Dim retval As Double

        With objVLAX
            .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
            .SetLispSymbol("point", point)
            retval = .EvalLispExpression("(vlax-curve-getDistAtPoint (handent handle) point)")
            .NullifySymbol({"handle", "point"})
        End With
        GetDistanceAtPoint = retval

    End Function

    Public Function GetFirstDerivative(Param As Double) As ClsVector3d
        Dim retval As Object, rtn As New ClsVector3d
        With objVLAX
            .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
            .SetLispSymbol("param", Param)
            .EvalLispExpression("(setq lst (vlax-curve-getFirstDeriv (handent handle) param))")
            retval = .GetLispList("lst")
            .NullifySymbol({"handle", "param", "lst"})
        End With
        rtn.CreateByXyz(retval(0), retval(1), retval(2))
        Return rtn
    End Function

    Public Function GetParameterAtDistance(Dist As Double) As Double

        Dim retval As Double

        With objVLAX
            .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
            .SetLispSymbol("dist", Dist)
            retval = .EvalLispExpression("(vlax-curve-getParamAtDist (handent handle) dist)")
            .NullifySymbol({"handle", "dist"})
        End With
        Return retval

    End Function

    Public Function GetParameterAtPoint(point As Object) As Double
        Dim retval As Double
        With objVLAX
            .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
            .SetLispSymbol("point", point)
            retval = .EvalLispExpression("(vlax-curve-getparamAtPoint (handent handle) point)")
            .NullifySymbol({"handle", "point"})
        End With
        Return retval

    End Function

    Public Function GetPointAtDistance(Dist As Double) As ClsPoint3d
        Dim retval As Object, pt As New ClsPoint3d
        With objVLAX
            .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
            .SetLispSymbol("dist", Dist)
            .EvalLispExpression("(setq lst (vlax-curve-getPointAtDist (handent handle) dist))")
            retval = .GetLispList("lst")
            .NullifySymbol({"handle", "dist", "lst"})
        End With
        pt.CreatorByVariant(retval)
        Return pt
    End Function

    Public Function GetPointAtParameter(Param As Double) As ClsPoint3d

        Dim retval As Object, pt As New ClsPoint3d

        With objVLAX
            .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
            .SetLispSymbol("param", Param)
            .EvalLispExpression("(setq lst (vlax-curve-getPointAtParam (handent handle) param))")
            retval = .GetLispList("lst")
            .NullifySymbol({"handle", "param", "lst"})
        End With
        pt.CreatorByVariant(retval)
        Return pt
    End Function

    Public Function GetSecondDerivative(Param As Double) As ClsVector3d
        Dim retval As Object, rtn As New ClsVector3d
        With objVLAX
            .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
            .SetLispSymbol("param", Param)
            .EvalLispExpression("(setq lst (vlax-curve-getSecondDeriv (handent handle) param))")
            retval = .GetLispList("lst")
            .NullifySymbol({"handle", "param", "lst"})
        End With
        rtn.CreatorByVariant(retval)
        Return rtn

    End Function

    Public ReadOnly Property Length() As Double
        Get
            Dim retval As Double
            With objVLAX
                .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
                .EvalLispExpression("(setq curve (handent handle))")
                retval = .EvalLispExpression("(vlax-curve-getDistAtParam curve (vlax-curve-getEndParam curve))")
                .NullifySymbol({"handle", "curve"})
            End With
            Return retval
        End Get
    End Property


    Public ReadOnly Property IsPeriodic() As Boolean
        Get
            Dim retval As Boolean
            With objVLAX
                .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
                retval = .EvalLispExpression("(vlax-curve-isPeriodic (handent handle))")
                .NullifySymbol({"handle"})
            End With
            Return retval
        End Get
    End Property

    Public ReadOnly Property IsPlanar() As Boolean
        Get
            Dim retval As Boolean
            With objVLAX
                .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
                retval = .EvalLispExpression("(vlax-curve-isPlanar (handent handle))")
                .NullifySymbol({"handle"})
            End With
            Return retval
        End Get
    End Property

    Public ReadOnly Property StartPoint() As ClsPoint3d
        Get
            Dim retval As Object, rtn As New ClsPoint3d
            With objVLAX
                .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
                .EvalLispExpression("(setq lst (vlax-curve-getStartPoint (handent handle)))")
                retval = .GetLispList("lst")
                .NullifySymbol({"handle", "lst"})
            End With
            rtn.CreatorByVariant(retval)
            Return rtn
        End Get
    End Property


    Public Function GetClosestPointToProjection(point As Object, Normal As Object, Optional Extend As Boolean = False) As ClsPoint3d

        Dim retval As Object, pt As New ClsPoint3d

        With objVLAX
            .SetLispSymbol("handle", mvarEntity.AcadObject.Handle)
            .SetLispSymbol("givenPt", point)
            .SetLispSymbol("normal", Normal)
            If Extend Then .EvalLispExpression("(setq ext T)")
            .EvalLispExpression("(setq lst (vlax-curve-getClosestPointToProjection (handent handle) givenPt normal ext))")
            retval = .GetLispList("lst")
            .NullifySymbol({"handle", "lst", "normal", "ext", "givenPt"})
        End With
        pt.CreatorByVariant(retval)
        Return pt
    End Function

    Protected Overrides Sub Finalize()
        objVLAX = Nothing
        MyBase.Finalize()
    End Sub
End Class


